<?php

namespace app\common\repositories\system\common;


use app\common\dao\system\common\ProblemDao;
use app\common\repositories\BaseRepository;
use FormBuilder\Factory\Elm;
use think\exception\ValidateException;
use think\facade\Route;

/**
 * Class FeedbackRepository
 * @package app\common\repositories\user
 * @author fupeng
 * @day 2023/10/7
 * @mixin ProblemDao
 */
class ProblemRepository extends BaseRepository
{
    /**
     * FeedbackRepository constructor.
     * @param ProblemDao $dao
     */
    public function __construct(ProblemDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User: fupeng
     * Date: 2023/10/7
     */
    public function getList(array $where, $page, $limit)
    {
        $query = $this->dao->search($where)->with(['type' => function($query){
            $query->field('common_problem_type_id,cate_name');
        }]);
        $count = $query->count();
        $list = $query->page($page, $limit)->select();
        return compact('count', 'list');
    }

    public function switchStatus(int $id,int $status)
    {
        $this->dao->update($id,['is_show' => $status]);
    }


    /**
     * @param array $where
     * @param $page
     * @param $limit
     * @param $orderBy
     * @param $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * User: fupeng
     * Date: 2023/10/7
     */
    public function getApiList(array $where, $page, $limit, $orderBy = null ,$field ='*')
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $query = $query->page($page, $limit)->setOption('field', [])->field($field);
        if($orderBy){
            $query->order($orderBy);
        }
        $list = $query->select();
        return compact('count', 'list');
    }

}
